﻿using AutoMapper;
using DFGK.WMS.Common.Extensions;
using DFGK_WMS.Context;
using DFGK_WMS.IRepository;
using DFGK_WMS.Model.DTO;
using DFGK_WMS.Model.Enum;
using DFGK_WMS.Model.Models;
using NetTaste;

namespace DFGK_WMS.Repository
{
    public class StackingStorageRepository : BaseRepository<TStackingStorage>, IStackingStorageRepository
    {
        public StackingStorageRepository(DFGK_DB context, IMapper mapper) : base(context, mapper)
        {

        }    


        /// <summary>
        /// 入库解绑
        /// </summary>
        /// <param name="palletBarcode"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public bool HandleUnbindOfInstorage(string palletBarcode, out string msg)
        {
            msg = string.Empty;

            TStackingStorage? stackStorage = Context.TStackingStorages
                .FirstOrDefault(t => t.PalletBarcode == palletBarcode && t.Status == 1);
            if (stackStorage is null)
            {
                msg = "查无匹配数据绑定记录，托盘号: " + palletBarcode;
                return false;
            }
            List<TPackageDetail> packageDetails = Context.TPackageDetails
                .Where(t => t.Status == 1 && t.StackingStorageId == stackStorage.Id).ToList();
            if (string.IsNullOrWhiteSpace(stackStorage.ParentPalletBarcode))
            {
                if (packageDetails == null || packageDetails.Count == 0)
                {
                    msg = "查无匹配数据绑定记录，托盘号: " + palletBarcode;
                    return false;
                }
            }
            TInventory? inventory = Context.TInventories.SingleOrDefault(it => it.PalletBarcode == palletBarcode);
            if (inventory == null)
            {
                msg = "该箱子已在库，无法进行解绑，托盘号: " + palletBarcode;
                return false;
            }
            TReceivingOrderDetail? orderDetail = Context.TReceivingOrderDetails.SingleOrDefault(it => it.Id == stackStorage.RelatedOrderDetailId);
            if (orderDetail == null)
            {
                msg = "该入库单的明细不存在,入库单明细ID为" + stackStorage.RelatedOrderDetailId;
                return false;
            }

            using var transaction = Context.Database.BeginTransaction();
            if (string.IsNullOrWhiteSpace(stackStorage.ParentPalletBarcode))
            {

                Context.TStackingStorages.Remove(stackStorage);

                if (Context.SaveChanges() == 0)
                {
                    msg = "更新包装明细状态为失效状态失败";
                    Context.Database.RollbackTransaction();
                    return false;
                }
            }
            Context.TStackingStorages.Remove(stackStorage);
            if (Context.SaveChanges() == 0)
            {
                msg = "删除组垛数据失败";
                Context.Database.RollbackTransaction();
                return false;
            }
            if (orderDetail.BillType != (int)EmInBillType.EmptyPalletIn && packageDetails != null && packageDetails.Count > 0)
            {
                List<string> barcodes = packageDetails.Where(t => t.PackageClass == 2).Select(t => t.Barcode).Distinct().ToList();
                if (barcodes != null && barcodes.Count > 0)
                {
                    List<TReceivingGoodsDetail> goodsDetails = Context.TReceivingGoodsDetails
                          .Where(it => barcodes.Contains(it.Barcode) && it.OrderDetailId == orderDetail.Id).ToList();

                    foreach (var item in goodsDetails)
                    {
                        item.Status = 0;
                        item.PackageBarcode = "";
                        item.PalletBarcode = "";
                    }

                    Context.TReceivingGoodsDetails.UpdateRange(goodsDetails);

                    if (Context.SaveChanges() == 0)
                    {
                        msg = "修改收货单明细表收货状态失败";
                        Context.Database.RollbackTransaction();
                        return false;
                    }
                }
                orderDetail.StackedQty = orderDetail.StackedQty - stackStorage.Qty;
                Context.TReceivingOrderDetails.Update(orderDetail);
                if (Context.SaveChanges() == 0)
                {
                    msg = "修改收货单明细表收货数量失败";
                    Context.Database.RollbackTransaction();
                    return false;
                }



            }
            transaction.Commit();

            return true;
        }
    }
}
